package com.example.shengyin.frontController;

import com.alibaba.fastjson2.JSON;
import com.example.shengyin.entity.Article;
import com.example.shengyin.entity.ArticleSimple;
import com.example.shengyin.entity.Page;
import com.example.shengyin.service.ArticleService;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.*;


@RestController
@RequestMapping("/front/index")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api(tags = "FrontIndexController", description = "首页")
public class FrontIndexController
{
    @Autowired
    private ArticleService service;
    @Autowired
    private RedisTemplate redisTemplate;
    public Map ReMap(){
        Map<String,List<String>> map=new HashMap<>();
        map.put("公司简介 ComProfile",new ArrayList<>(Arrays.asList("CompanyInfo")));
        map.put("媒体报道 Media",new ArrayList<>(Arrays.asList("媒体报道 Media")));
        map.put("交流合作 Cooperate",new ArrayList<>(Arrays.asList("交流合作")));
        map.put("大事记",new ArrayList<>(Arrays.asList("大事记")));
        map.put("公司新闻 Comnews",new ArrayList<>(Arrays.asList("开业活动","国际交流","员工活动","亲子活动","公益讲座","公益活动","家长培训","社会关注","全部")));
        map.put("活动讲座 Lecture",new ArrayList<>(Arrays.asList("热门推荐","专题精选","门店信息")));
        map.put("言语科普",new ArrayList<>(Arrays.asList("言语科普")));
        map.put("课程介绍",new ArrayList<>(Arrays.asList("课程介绍")));
        return map;
    }


    @ApiOperation("详细的文章内容")
    @GetMapping(value = "/detail/{id}")
    public Result select(@PathVariable(value = "id") Long id)
    {
        Article article = service.selectById(id);
        System.out.println(JSON.toJSONString(article));
        if(article==null)
            return Result.error("没有找到数据");
        article.setVisittimes(article.getVisittimes()+1);
        service.update(article);
        return Result.success(article);
    }

    @ApiOperation("首页->言语科普->热文(number是数据条数)")
    @GetMapping(value = "/kepu_hot/{number}")
    public Result selecthot(@PathVariable(value = "number")int number)
    {
        List<ArticleSimple> article1 = service.selecthot(number);
        return Result.success(article1);
    }
    @ApiOperation("首页->言语科普->最新(number是数据条数)")
    @GetMapping(value = "/kepu_newest/{number}")
    public Result kepu_newest(@PathVariable(value = "number")int number)
    {
        List<ArticleSimple> article2 = service.selectnewwest(number);
        return Result.success(article2);
    }
    @ApiOperation("首页->言语科普->家长推荐(number是数据条数)")
    @GetMapping(value = "/kepu_recomend/{number}")
    public Result kepu_recomend(@PathVariable(value = "number")int number)
    {
        List<ArticleSimple> article2 = service.selectnewwest(number);
        return Result.success(article2);
    }




    @ApiOperation("首页->公司新闻")
    @GetMapping(value = "/findComnews/{page}/{limit}")
    public Result findConmnews(@PathVariable(value = "page") int page,
                                   @PathVariable(value = "limit")int limit)
    {
        Page page1=new Page(page,limit);
        int comnews = service.countTotall("公司新闻", null, null, null, null);
        List<ArticleSimple> articles = service.articleList2(page1, "公司新闻", null, null, null, null);
        if(comnews==0||articles==null)
            return Result.success(page,"没有数据");
        page1.setCount(comnews);
        page1.setData(articles);
        return Result.success(page1);
    }

    @ApiOperation("首页->活动讲座")
    @GetMapping(value = "/findLecture/{page}/{limit}")
    public Result findLecture(@PathVariable(value = "page") int page,
                              @PathVariable(value = "limit")int limit)
    {
        Page page1=new Page(page,limit);
        int comnews = service.countTotall(null, "Lecture", null, null, null);
        List<ArticleSimple> articles = service.articleList2(page1, null, "Lecture", null, null, null);
        if(comnews==0||articles==null)
            return Result.success(page1,"没有数据");
        page1.setCount(comnews);
        page1.setData(articles);
        return Result.success(page1);
    }
    @ApiOperation("首页->交流合作")
    @GetMapping(value = "/findCooperate/{page}/{limit}")
    public Result findCooperate(@PathVariable(value = "page") int page,
                                @PathVariable(value = "limit")int limit)
    {
        Page page1=new Page(page,limit);
        int comnews = service.countTotall(null, "Cooperate", null, null, null);
        List<ArticleSimple> articles = service.articleList2(page1, null, "Cooperate", null, null, null);
        if(comnews==0||articles==null)
            return Result.success(page1,"没有数据");
        page1.setCount(comnews);
        page1.setData(articles);
        return Result.success(page1);
    }
    @ApiOperation("首页->媒体报道")
    @GetMapping(value = "/findMedia/{page}/{limit}")
    public Result findMedia(@PathVariable(value = "page") int page,
                            @PathVariable(value = "limit")int limit)
    {
        Page page1=new Page(page,limit);
        int comnews = service.countTotall(null, "Media", null, null, null);
        List<ArticleSimple> articles = service.articleList2(page1, null, "Media", null, null, null);
        if(comnews==0||articles==null)
            return Result.success(page1,"没有数据");
        page1.setCount(comnews);
        page1.setData(articles);
        return Result.success(page1);
    }
}
